package com.example.yaun0608;


import android.content.ContentResolver;
import android.database.Cursor;
import android.net.DnsResolver;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;


public class Main2Activity extends AppCompatActivity {
    private ListView lv;
    public List<String> books;
    private ContentResolver resolver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        resolver=getContentResolver();
        lv=findViewById(R.id.lv);
        init();
    }


    public void init() {
        books = new ArrayList<>();
        String path = "content://com.example.yuan0608.MyProvider/books";
        Uri uri = Uri.parse(path);
        Cursor query = resolver.query(uri, null, null, null, null);
        if (query != null && query.getCount() > 0) {
            while (query.moveToNext()) {
                int id = query.getInt(0);
                String bookname = query.getString(1);
                String author = query.getString(2);
                this.books.add("书名："+bookname+"\n作者："+author);
            }
        }
        ListAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.activity_list_item,android.R.id.text1,books);
        lv.setAdapter(adapter);

    }
}
